
const id = window.localStorage.getItem('id')

const token = window.localStorage.getItem('token')

const onBox = document.querySelector('.on')
const offBox = document.querySelector('.off')
// const registBox = document.querySelector('.regist')
// const myLisBox = document.querySelector('.myLis')

if (!id) {
  onBox.classList.remove('active')
  offBox.classList.add('active')
  // myLisBox.classList.remove('active')
  // registBox.classList.add('active')
}
const xhr = new XMLHttpRequest()

xhr.open('GET', 'http://localhost:8888/users/info?id=' + id)

xhr.onload = function () {
  const res = JSON.parse(xhr.responseText)
  if (res.code === 401) {
    onBox.classList.remove('active')
    offBox.classList.add('active')
    // myLisBox.classList.remove('active')
    // registBox.classList.add('active')
  }

  // 拿到正常的用户信息了, 那么直接显示 您好 xxxx 个人中心 注销登录
  if (res.code === 1) {
    onBox.firstElementChild.innerText = res.info.nickname
    offBox.classList.remove('active')
    // registBox.classList.remove('active')
    onBox.classList.add('active')
    // myLisBox.classList.add('active')
  }
}
xhr.setRequestHeader('authorization', token)
xhr.send()

// 注销登录的点击事件
const logoutBtn = document.querySelector('.on>a')
logoutBtn.addEventListener('click', async () => {
  const res = await pAjax({
    url: 'http://localhost:8888/users/logout',
    data: { id },
    dataType: 'json'
  })

  // 根据 res.code 来操作刷新一下页面
  // 顺便把 lcoalStorage 内的信息也删除
  if (res.code === 1) {
    window.localStorage.removeItem('id')
    window.localStorage.removeItem('token')

    // 刷新页面
    window.location.reload()
  }
})